package xyz.kail.demo.consul.api;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.KeyValueConsulClient;
import com.ecwid.consul.v1.kv.model.GetValue;

import java.nio.charset.StandardCharsets;

/**
 * @author Kail
 * @since 2018/1/16.
 */
public class KvHelloMain {

    public static void main(String[] args) {

        ConsulRawClient consulRawClient = new ConsulRawClient("localhost", 8511);
        ConsulClient consulClient = new ConsulClient(consulRawClient);

        printValue(consulClient.getKVValue("123"));
        printValue(consulClient.getKVValue("345", QueryParams.Builder.builder().setDatacenter("dc2").setNear("123").setIndex(11L).setWaitTime(12L).build()));

    }

    static void printValue(Response<GetValue> kvValue) {
        System.out.println(kvValue.getConsulIndex());
        System.out.println(kvValue.getConsulLastContact());
        System.out.println(kvValue.getValue().getCreateIndex());
        System.out.println(kvValue.getValue().getDecodedValue(StandardCharsets.UTF_8));
        System.out.println(kvValue.getValue().getFlags());
        System.out.println(kvValue.getValue().getLockIndex());
        System.out.println(kvValue.getValue().getKey());
        System.out.println(kvValue.getValue().getValue());
        System.out.println();
        System.out.println();
        System.out.println();
    }

}
